package cn.zyq.ykb.service.advert.impl;

import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.advert.AdvertMapper;
import cn.zyq.ykb.beans.advert.Advert;
import cn.zyq.ykb.service.advert.AdvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.UUID;
import java.util.List;

/**
 * 悦推客广告Service业务层处理
 *
 * @author SystemAuth
 * @date 2024-09-28
 */
@Service
public class AdvertServiceImpl  extends BaseService implements AdvertService {

    @Autowired
    private AdvertMapper advertMapper;

    /**
     * 查询悦推客广告
     *
     * @param advertID 悦推客广告主键
     * @return 悦推客广告
     */
    @Override
    public Advert selectAdvertByAdvertID(Integer advertID) {
        Advert advert = advertMapper.selectByPrimaryKey(advertID);
        if (advert.getDeleted() == 1) {
            return null;
        }
        return advert;
    }

    /**
     * 查询悦推客广告
     *
     * @param serviceID 业务ID
     * @return 悦推客广告
     */
    @Override
    public Advert selectAdvertByServiceID(String serviceID) {
        Advert param = new Advert();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.advertMapper.selectOne(param);
    }

    /**
     * 查询悦推客广告列表
     *
     * @param pageParam
     * @return 悦推客广告
     */
    @Override
    public PageInfo<Advert> selectAdvertList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<Advert> list = this.advertMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询悦推客广告列表
     *
     * @param pageParam
     * @return 悦推客广告
     */
    @Override
    public List<Advert> selectAdvertAllList(PageParam pageParam) {
        return this.advertMapper.list(pageParam.getKv());
    }
    /**
     * 新增悦推客广告
     *
     * @param param 悦推客广告
     * @return 结果
     */
    @Override
    public int insertAdvert(Advert param) {
        this.setCreateBaseData(param, new User());
        return this.advertMapper.insertSelective(param);
    }

    /**
     * 修改悦推客广告
     *
     * @param param 悦推客广告
     * @return 结果
     */
    @Override
    public int updateAdvert(Advert param) {
        this.setUpdateBaseData(param,new User());
        return this.advertMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除悦推客广告
     *
     * @param advertIDs 需要删除的悦推客广告主键
     * @return 结果
     */
    @Override
    public int deleteAdvertByAdvertIDs(Integer[] advertIDs) {
        int i = 0;
        for (Integer advertID : advertIDs) {
            i += this.deleteAdvertByAdvertID(advertID);
        }
        return i;
    }

    /**
     * 删除悦推客广告信息
     *
     * @param advertID 悦推客广告主键
     * @return 结果
     */
    @Override
    public int deleteAdvertByAdvertID(Integer advertID) {
        Advert advert = this.selectAdvertByAdvertID(advertID);
        if (advert == null) {
            return 0;
        } else {
            Advert storeInfo = new Advert();
            storeInfo.setAdvertID(advertID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.advertMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }

    @Override
    public List<Advert> getList() {
        Advert advert = new Advert();
        advert.setDeleted(0);
        return advertMapper.select(advert);
    }
}
